package com.llb666.java.sortalgorithm;

import java.util.Arrays;

/**
 * Created by 斌~
 * 2021/4/1 16:50
 */
public class BubbleTest {
    /**
     * 冒泡排序
     * @param arrs 数组
     */
    private static void bubbleSort(int[] arrs){
        for (int i = arrs.length-1; i >0; i--) {
            for (int j = 0; j < i; j++) {
                if(arrs[j] > arrs[j+1]){
                    int temp = arrs[j];
                    arrs[j] = arrs[j+1];
                    arrs[j+1] = temp;
                }
            }
        }
    }

    /**
     *  选择排序
     * @param a 数组
     */
    private static void selectionSort(int[] a){
        for(int i = 0; i <  a.length - 1; i++){
            int min = i;
            for(int j = i + 1; j < a.length; j++){
                //将a[i]和a[i+1...N-1]中的最小元素交换
                if(a[j] < a[min]){//升序排列
                    min = j;
                }
            }
            if(min != i){
                int temp = a[i];
                a[i] = a[min];
                a[min] = temp;
            }
        }
    }

    public static void main(String[] args) {
        int[] arrs = {1,4,5,3,6,7,8,9,2};
//        BubbleTest.bubbleSort(arrs);
        BubbleTest.selectionSort(arrs);
        Arrays.stream(arrs).forEach(System.out::println);
    }
}
